using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vms.Web.Models;
using Vms.Web.Lib;

namespace Vms.Web.Areas.Lib.Controllers
{   
    public class ContentTxtController : Controller
    {
        private VmsWebContext context = new VmsWebContext();

        //
        // GET: /LIB_CONTENT_TXT/

        public ViewResult Index()
        {
            return View(context.LIB_CONTENT_TXTS.ToList());
        }

        //
        // GET: /LIB_CONTENT_TXT/Details/5

        public ViewResult Details(int id)
        {
            LIB_CONTENT_TXT lib_content_txt = context.LIB_CONTENT_TXTS.Single(x => x.Id == id);
            return View(lib_content_txt);
        }

        //
        // GET: /LIB_CONTENT_TXT/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /LIB_CONTENT_TXT/Create

        [HttpPost]
        public ActionResult Create(LIB_CONTENT_TXT lib_content_txt)
        {
            if (ModelState.IsValid)
            {
                string file = "lib_txt_" + DateTime.Now.Ticks+".txt";
                System.IO.StreamWriter sw = System.IO.File.CreateText(Utility.pathLibContentTxt+file);
                sw.Write(lib_content_txt.Txt);
                sw.Close();
                lib_content_txt.TxtFileName = file;

                context.LIB_CONTENT_TXTS.Add(lib_content_txt);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(lib_content_txt);
        }
        
        //
        // GET: /LIB_CONTENT_TXT/Edit/5
 
        public ActionResult Edit(int id)
        {
            LIB_CONTENT_TXT lib_content_txt = context.LIB_CONTENT_TXTS.Single(x => x.Id == id);
            return View(lib_content_txt);
        }

        //
        // POST: /LIB_CONTENT_TXT/Edit/5

        [HttpPost]
        public ActionResult Edit(LIB_CONTENT_TXT lib_content_txt)
        {
            if (ModelState.IsValid)
            {
                context.Entry(lib_content_txt).State = EntityState.Modified;

                string file = Utility.PathScreenContentTxt + lib_content_txt.TxtFileName;

                if (System.IO.File.Exists(file))
                {
                    System.IO.StreamWriter sw = System.IO.File.CreateText(file);
                    sw.Write(lib_content_txt.Txt);
                    sw.Close();
                }
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(lib_content_txt);
        }

        //
        // GET: /LIB_CONTENT_TXT/Delete/5
 
        public ActionResult Delete(int id)
        {
            LIB_CONTENT_TXT lib_content_txt = context.LIB_CONTENT_TXTS.Single(x => x.Id == id);

            return View(lib_content_txt);
        }

        //
        // POST: /LIB_CONTENT_TXT/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            LIB_CONTENT_TXT lib_content_txt = context.LIB_CONTENT_TXTS.Single(x => x.Id == id);

            string file = Utility.pathLibContentTxt + lib_content_txt.TxtFileName;

            if (System.IO.File.Exists(file))
            {
                System.IO.File.Delete(file);
            }

            context.LIB_CONTENT_TXTS.Remove(lib_content_txt);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                context.Dispose();
            }
            base.Dispose(disposing);
        }
        public ActionResult SearchContentTxt(string searchString)
        {
            var lib_content_txts = from m in context.LIB_CONTENT_TXTS
                                   select m;

            if (!String.IsNullOrEmpty(searchString))
            {
                lib_content_txts = lib_content_txts.Where(s => s.Txt.Contains(searchString));
            }

            return View(lib_content_txts);
        }
    }
}